package com.situ.car.controller;

import com.situ.car.pojo.entity.Car;
import com.situ.car.pojo.query.BrandCarQuery;
import com.situ.car.pojo.query.CarQuery;
import com.situ.car.service.ICarService;
import com.situ.car.service.Impl.CarServiceImpl;
import com.situ.car.util.MyBeanUtils;
import com.situ.car.util.PageInfo;
import com.situ.car.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/car")
public class CarController extends BaseController{
    private ICarService carService = new CarServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CarController.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByBrandCar":
                selectByBrandCar(req, resp);
                break;
            case "selectByCar":
                selectByCar(req, resp);
                break;
            case "selectByCarList":
                selectByCarList(req, resp);
                break;
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "selectByCarPage":
                selectByCarPage(req, resp);
                break;
        }
    }

    private void selectByCarPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.selectByCarPage");
        BrandCarQuery brandCarQuery = MyBeanUtils.copyToBean(req, BrandCarQuery.class);
        PageInfo pageInfo = carService.selectByCarPage(brandCarQuery);
        toJSON(resp, pageInfo);
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.selectByPage");
        CarQuery carQuery = MyBeanUtils.copyToBean(req, CarQuery.class);
        PageInfo pageInfo = carService.selectByPage(carQuery);
        toJSON(resp, pageInfo);
    }

    private void selectByCarList(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.selectByCarList");
        List<Car> list = carService.selectByCarList();
        toJSON(resp, Result.ok(list));
    }

    private void selectByCar(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.selectByCar");
        String id = req.getParameter("carId");
        Car car = carService.selectByCar(Integer.parseInt(id));
        toJSON(resp, Result.ok(car));
    }

    private void selectByBrandCar(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.selectByBrandCar");
        String brandId = req.getParameter("brandId");
        List<Car> list = carService.selectByBrandCar(Integer.parseInt(brandId));
        toJSON(resp, Result.ok(list));

    }
}
